from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class Base:
    def __init__(self,driver):
        """
        :param driver: 驱动
        """
        self.driver = driver
        # 选取元素的方式
        self.loctors_method = {
            "id":By.ID,
            "name":By.NAME,
            "xpath":By.XPATH,
            "class name":By.CLASS_NAME,
            "link text":By.LINK_TEXT,
            "css":By.CSS_SELECTOR,
            "tag name":By.TAG_NAME,
            "partial link text":By.PARTIAL_LINK_TEXT
        }

    def find_element(self,loc_method,loc):
        """
        :param loc_method: 要选择的定位方式
        :param loc: 元素值
        :return:
        """
        try:
            element = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((self.loctors_method[loc_method],loc)))
            return element
        except Exception as e:
            return "元素超时，没有找到：%s"%e
    def find_elements(self,loc_method,loc,num):
        """
        :param loc_method: 要选择的定位方式
        :param loc: 元素值
        :param num: 要操作第几个索引位置的元素
        :return:
        """
        try:
            ele = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((self.loctors_method[loc_method],loc)))[num]
            return ele
        except Exception as e:
            return "元素超时，没有找到：%s"%e
    # 点击元素
    def click_element(self,loc_method,loc):
        """
        :param loc_method: 元素方式
        :param loc: 元素值
        :return:
        """
        self.find_element(loc_method,loc).click()
    # 点击元素中的第几个
    def click_elements(self, loc_method, loc,num):
        self.find_elements(loc_method, loc,num).click()
    def get_text(self,loc_method,loc):
        """
        :param loc_method: 选择元素方式
        :param loc: 定位元素内容
        :return:
        """
        ele = self.find_element(loc_method,loc)
        text = ele.text
        return text
    def get_texts(self,loc_method,loc ,num):
        """
        :param loc_method: 选择元素方式
        :param loc: 定位元素内容
        :return:
        """
        ele = self.find_elements(loc_method,loc,num)
        text = ele.text
        return text
    # 句柄切换方法
    def win_switch(self,num):
        """
        :param num: 要跳进第几个句柄位置
        :return:
        """
        win = self.driver.window_handles
        self.driver.switch_to.window(win[num])
    # alert弹窗处理
    def alert_switch(self,type:int):
        """
        :param type: 枚举值，传入1，accept，传入2，dismiss
        :return:
        """
        alret = self.driver.switch_to.alert
        if type == 1:
            alret.accept()
        elif type == 2:
            alret.dismiss()
    # iframe内联框架处理
    def iframe_switch(self,loc_method,loc,num):
        """
        :param loc_method: 定位方式
        :param loc: 定位元素
        :param num: 具体第几个要操作的
        :return:
        """
        iframe = self.find_elements(loc_method,loc,num)
        self.driver.switch_to.frame(iframe)
    # 输入文本内容
    def input_text(self,loc_method,loc,content):
        text = self.find_element(loc_method, loc)
        # 先清除：
        text.clear()
        # 再输入
        text.send_keys(content)

        # 输入文本内容
    def input_texts(self, loc_method, loc, num,content):
        text = self.find_elements(loc_method, loc,num)
        # 先清除：
        text.clear()
            # 再输入
        text.send_keys(content)






















